package com.zitangkou.common.global;

import com.zitangkou.common.enums.RespEnum;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 全局响应参数类
 *
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/8/14 17:32
 */
@Data
@Accessors(chain = true)
public class GlobalResp<T> {
    /**
     * 接口调用是否成功，0：成功，1：失败
     */
    private boolean success;
    /**
     * 接口调用结果编号
     */
    private Integer code;
    /**
     * 接口调用结果信息
     */
    private String msg;
    /**
     * 接口调用结果数据
     */
    private T data;

    public static <T> GlobalResp<T> success() {
        return success((T) null);
    }

    public static <T> GlobalResp<T> success(T data) {
        return new GlobalResp()
                .setData(data)
                .setCode(RespEnum.SUCCESS.getCode())
                .setMsg(RespEnum.SUCCESS.getMsg())
                .setSuccess(true);
    }

    public static <T> GlobalResp<T> success(GlobalException e) {
        return new GlobalResp()
                .setData(null)
                .setCode(e.getCode())
                .setMsg(e.getMsg())
                .setSuccess(true);
    }

    public static <T> GlobalResp<T> success(RespEnum result) {
        return success(result, null);
    }

    public static <T> GlobalResp<T> success(RespEnum result, T data) {
        return new GlobalResp()
                .setData(data)
                .setCode(result.getCode())
                .setMsg(result.getMsg())
                .setSuccess(true);
    }

    public static <T> GlobalResp<T> fail(RespEnum result) {
        return fail(result, null);
    }

    public static <T> GlobalResp<T> fail(RespEnum result, Throwable e) {
        return new GlobalResp()
                .setData(e)
                .setCode(result.getCode())
                .setMsg(result.getMsg())
                .setSuccess(false);
    }
}
